package com.ordersystem.controller;

import com.ordersystem.domain.Order;
import com.ordersystem.domain.OrderManageData;
import com.ordersystem.domain.Result;
import com.ordersystem.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author : asus1020
 * @version : [v1.0]
 * @className : OrderController
 * @description : [描述说明该类的功能]
 * @createTime : [2022/3/26 17:40]
 * @updateUser : asus1020
 * @updateTime : [2022/3/26 17:40]
 * @updateRemark : [描述说明本次修改内容]
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/addOrder")//添加订单
    public Result addOrder(@RequestBody Order order){
        Result result = new Result();
        int total = orderService.addOrder(order);
        if (total>0){
            result.setStatus(200);
            result.setMessage("添加订单成功");
        }else {
            result.setStatus(500);
            result.setMessage("添加订单失败");
        }
        return result;


    }

    @PutMapping("/payOrder/{oid}/{state}")//订单付款
    public Result payOrder(@PathVariable("oid") Integer oid, @PathVariable("state") Integer state){
        Result result = new Result();
        int total = orderService.parOrder(oid,state);
        if (total>0){
            result.setStatus(200);
            result.setMessage("付款成功");
        }else {
            result.setStatus(500);
            result.setMessage("付款失败");
        }
        return result;
    }
    @PutMapping("/finishOrder/{oid}/{state}")//订单结束
    public Result finishOrder(@PathVariable("oid") Integer oid, @PathVariable Integer state){
        Result result = new Result();
        boolean status=false;
        if (state==1)status=true;
        int total = orderService.finishOrder(oid,status);
        if (total>0){
            result.setStatus(200);
            result.setMessage("订单完成");
        }else {
            result.setStatus(500);
            result.setMessage("订单结束失败");
        }
        return result;
    }
    @GetMapping("/findOrders")//查询所有订单
    public Result findOrders(String query, Integer type, Integer pagesize, Integer pagenum){
        Result result = new Result();
        Map<String, Object> orders = orderService.findOrders(query,type,pagenum,pagesize);
        if (orders!=null){
            result.setStatus(200);
            result.setMessage("查询订单成功");
            result.setData(orders);
        }else {
            result.setStatus(500);
            result.setMessage("查询订单失败");
        }
        return result;
    }
    @GetMapping("/findOrdersByUid/{uid}")
    public Result findOrdersByUid(@PathVariable("uid") Integer uid){
        Result result = new Result();
        List<Order> ordersByUid = orderService.findOrdersByUid(uid);
        if (ordersByUid!=null){
            result.setStatus(200);
            result.setData(ordersByUid);
        }else {
            result.setStatus(500);
            result.setMessage("查询历史订单失败");
        }
        return result;
    }
    @GetMapping("/orderManageData/{dataNum}")
    public Result getOrderManageData(@PathVariable("dataNum") Integer dataNum){
        Result result = new Result();
        List<OrderManageData> orderManageData = orderService.getOrderManageData(dataNum);
        if (orderManageData!=null){
            result.setStatus(200);
            result.setData(orderManageData);
        }else {
            result.setStatus(500);
            result.setMessage("获取数据失败");
        }


        return result;
    }

}
